/**
 * Created by MSI on 2017/9/4.
 */
var roomid;
$(document).ready(function () {
    init();
    console.log(GetQueryString("roomid"))
    roomid=GetQueryString("roomid");
    $('#roomid').text(GetQueryString("roomid"))
    getNum()
});

function GetQueryString(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}
function hasFollow() {
    console.log(name+"and"+roomid)
    $.ajax({
        url:url+"person/hasFollow.do",
        data:{username:name,roomid:roomid},
        type:"get",
        datatype:"json",
        success:function (data) {
            console.log("hasFollow:"+data)
            if(data=="success")
            {
                $("#unfollow").removeClass('hide');
            }
            else
            {
                $("#follow").removeClass('hide');
            }
        }
    })
}
var websocket = null;
var name="";
function getUserName() {
    $.ajax({
        url:url+"getSession.do",
        type:"get",
        data:"",
        datatype:"json",
        success:function (data) {
            var data=JSON.parse(data);
            if(data.result=="success")
            {
                var user=data.user;
                name=user.username;
                hasFollow()
            }else{
                name="";
                $('#send').attr("disabled",true);
                $('#textbox').attr("disabled",true);
            }
        }
    })
}
var roomname="";
var status="";
var username="";
var mastername="";
var point="";
var roomid;
function init() {

    $.ajax({
        url:url+'person/getRoomInfo',
        data:{roomid:GetQueryString("roomid")},
        type:"get",
        datatype:"json",
        success:function (data) {
            if(data!="null") {
                roomInfoinit(data);
                getUserName()
            }
        }
    })
}
function send() {
    var message = $("#textbox").val();
    console.log("send"+message);
    $("#message").append("<h4 class='right'>"+"<span class='glyphicon glyphicon-user'></span>"+" "+username+":"+message+"</h4>");
    websocket.send(username+":"+message);
    $("#textbox").val("");
}
function showMessage(data){
    $("#message").append("<h4 class='right'>"+"<span class='glyphicon glyphicon-user'></span>"+" "+data+"</h4>")
    console.log("receive"+data);
}
function closeWebSocket() {
    websocket.close();
}
function Openwebsocket(num) {
    if ('WebSocket' in window) {
        websocket = new WebSocket(ws+"websocket/"+num);

    }
    else {
        alert('当前浏览器版本过低不支持websocket!!!')
    }
    websocket.onerror = function () {
        setMessage("WebSocket连接发生错误");
    };
    websocket.onopen = function () {
    };
    websocket.onmessage = function (event) {
        showMessage(event.data);
    };
    websocket.onclose = function () {
        setMessage("WebSocket连接关闭");
    };


}

function roomInfoinit(roomInfo){
    console.log(roomInfo)
    roomname=roomInfo.roomname;
    status=roomInfo.status;
    mastername=roomInfo.username;
    point=roomInfo.point;
    picture=roomInfo.picturesrc;
    roomid = roomInfo.roomid;
    console.log(picture);
    $("#roomname").text(roomname);
    $("#name").text(mastername);
    $("#picture").attr("src",picture);
    console.log(status)
    if(status==0){
        $("#status").text("当前未直播");
    }else{
        $("#status").text("直播中");
    }
    jwplayer('mediaplayer').setup({
        file: "rtmp://47.94.164.238:1935/live/"+roomInfo.roomkey,//调用视频文件
        autostart:true,//自动播放
        width: "100%",
        height: "585"
    });
    Openwebsocket(roomid)

    videoinit(mastername);
    getNum()
}
function videoinit(name) {
    var list;
    $.ajax({
        url:url+"/person/getMyVideo.do",
        data:{username:name},
        type:"get",
        datatype:"json",
        success:function (data) {
            data=JSON.parse(data);
            console.log("长度"+data.length)
            if(data.length!=0){
                initList(data,name);
            }else{
                $("#owner").text(username+"未发布视频");
                $("#videolist").hide();
            }
        }
    });
}
function initList(data,username) {
    $("#owner").text(username+"所发布的视频");
    for(var i=0,j=data.length;i<j;i++){
        var id=data[i].videoid;
        console.log(id);
        console.log(url);
        $('tbody').append("<tr>" +
            "<td>"+data[i].videoname+"</td>" +
            "<td>"+data[i].date+"</td>" +
            "<td><a href='/static/project/media.html?"+id+"'>观看</a></td>" +
            "<td class='hide'><input data='"+i+"'>data[i].videosrc</label></td>"+
            "</tr>")
    }
}

//websocket

$('#follow').on('click',function () {
    $.ajax({
        url:url+"person/activityFollow.do",
        data:{username:name,roomid:roomid},
        datatype:"json",
        type:"get",
        success:function (data) {
            if(data=="success")
            {
                $("#follow").addClass('hide')
                $("#unfollow").removeClass('hide');
            }
        }
    })
})



$('#unfollow').on('click',function () {
    $.ajax({
        url:url+"person/activityUnFollow.do",
        data:{username:name,roomid:roomid},
        datatype:"json",
        type:"get",
        success:function (data) {
            if(data=="success")
            {
                $("#follow").removeClass('hide')
                $("#unfollow").addClass('hide');
            }
        }
    })
})


function getNum() {
    $.ajax({
        url:url+'person/getNum.do',
        data:{roomid:roomid},
        type:'get',
        datatype:"json",
        success:function (data) {
            console.log(data)
            $('#num').text(data);
        }
    })
}
